import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect:'/home'
  }
  ,
  {
    path: '/about',
    name: 'about',
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  }
  ,
  {
    path: '/home', // 唯一值
    name: 'home', // 唯一值
    meta:{
      isKeepAlive:false
    },
    //  提高项目运行性能
    component: () => import(/* webpackChunkName: "home" */ '../views/home/index.vue')
  }
  ,
  {
    path: '/home/searchaddress', // 唯一值
    name: 'SearchAddress', // 唯一值
   
    //  提高项目运行性能
    component: () => import(/* webpackChunkName: "home" */ '../views/home/addressSearch/index.vue')
  }
  ,
  {
    path: '/home/goodsSearch', // 唯一值
    name: 'SearchGoods', // 唯一值
   
    //  提高项目运行性能
    component: () => import(/* webpackChunkName: "home" */ '../views/home/goodsSearch/index.vue')
  }
  ,
  {
    path: '/home/goodsList', // 唯一值
    name: 'GoodsList', // 唯一值
   
    //  提高项目运行性能
    component: () => import(/* webpackChunkName: "home" */ '../views/home/goodsList/index.vue')
  }
  ,
  {
    path: '/topic', // 唯一值
    name: 'topic', // 唯一值
   
    //  提高项目运行性能
    component: () => import(/* webpackChunkName: "topic" */ '../views/topic/index.vue')
  }
  ,
  {
    path: '/topic/detail', // 唯一值
    name: 'topicDetail', // 唯一值
   
    //  提高项目运行性能
    component: () => import(/* webpackChunkName: "topic" */ '../views/topic/Detail.vue')
  }
  ,
  {
    path: '/category', // 唯一值
    name: 'category', // 唯一值
    meta:{
      isKeepAlive:true
    },
    //  提高项目运行性能
    component: () => import(/* webpackChunkName: "category" */ '../views/category/index.vue')
  }
  ,
  {
    path: '/cart', // 唯一值
    name: 'cart', // 唯一值
  
    //  提高项目运行性能
    component: () => import(/* webpackChunkName: "cart" */ '../views/cart/index.vue')
  }
  ,
  {
    path: '/my', // 唯一值
    name: 'my', // 唯一值
   
    //  提高项目运行性能
    component: () => import(/* webpackChunkName: "my" */ '../views/my/index.vue')
  }
  ,
  {
    path: '/goodslist', // 唯一值
    name: 'CategoryGoodsList', // 唯一值
   
    //  提高项目运行性能
    component: () => import('../views/goodsList/index.vue')
  }
  ,
  {
    path: '/goodsDetail/:id', // 路由传参 唯一值
    name: 'GoodsDetail', // 唯一值
   
    //  提高项目运行性能
    component: () => import('../views/goodsDetail/index.vue')
  }
  ,
  {
    path: '/order', // 唯一值
    name: 'Order', // 唯一值
   
    //  提高项目运行性能
    component: () => import('../views/order/index.vue')
  }
  ,
  {
    path: '/address', // 唯一值
    name: 'Address', // 唯一值
   
    //  提高项目运行性能
    component: () => import('../views/address/index.vue')
  }
  ,
  {
    path: '/addressDetail', // 唯一值
    name: 'AddressDetail', // 唯一值
   
    //  提高项目运行性能
    component: () => import('../views/address/Detail.vue')
  }
]

const router = new VueRouter({
  routes,
  scrollBehavior (to, from, savedPosition) {
    console.log("to=>",to);
    // 有滚动距离 并且是同一个商品
    if(to.meta.y>0 && to.meta.goodsId === to.params.id){
      return new Promise((resolve, reject) => {
        setTimeout(() => {
          resolve({ x: 0, y: to.meta.y })
        }, 500)
      })
    }else if(savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

export default router
